package com.blb.controller;

import com.blb.pojo.BranchCompany;
import com.blb.pojo.PageEntity;
import com.blb.service.BranchCompanyService;
import com.blb.util.AddressLngLatExchange;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@CrossOrigin(origins = "*")
@Api(tags = "联系方式")
public class BranchCompanyController {

    @Autowired
    private BranchCompanyService branchCompanyService;
    @Autowired
    private AddressLngLatExchange addressLngLatExchange;

    @ApiOperation(value = "联系我们分页")
    @GetMapping("/branchCompany/{current}/{size}")
    public ResponseEntity<PageEntity<BranchCompany>> findAll(@ApiParam(name = "current",value = "当前页",required = true) @PathVariable("current") int current,
                                                             @ApiParam(name = "size",value = "每页数量",required = true) @PathVariable("size") int size) {
        current=current<=0?1:current;
        PageEntity<BranchCompany> branchCompanyPage = branchCompanyService.findBranchCompanyPage(current, size);
        return ResponseEntity.ok(branchCompanyPage);
    }

    /**
     * 获取所有的地址信息
     * @return
     */
    @ApiOperation(value = "获取所有的地址信息")
    @GetMapping("/contact")
    public List<BranchCompany> queryAll(){
        List<BranchCompany> branchCompanies = branchCompanyService.queryAllByLimit();
        return branchCompanies;
    }

    /**
     * 获取所有区域信息
     * @return
     */
    @ApiOperation(value = "获取所有区域信息")
    @GetMapping("/contactList")
    public List<String> queryAllContact(){
        List<String> list = branchCompanyService.queryLocation();
        return list;
    }

    /**
     * 通过id获取对应地址的经纬度
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id获取对应地址的经纬度")
    @GetMapping("/query/{id}")
    public String query(@ApiParam(name = "id",value = "分支id",required = true)
                            @PathVariable Integer id){
        String lngLat=null;
        BranchCompany branchCompany = branchCompanyService.queryById(id);
        if (branchCompany!=null){
            lngLat = addressLngLatExchange.getLngLat(branchCompany.getCompanyAddress());
        }
        return lngLat;
    }

}
